package com.neuedu.servlet;


import com.google.gson.Gson;
import com.neuedu.pojo.ShopType;
import com.neuedu.service.Imp.ShopTypeServiceImp;
import com.neuedu.service.ShopTypeService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/shopType")
public class ShopTypeServlet extends HttpServlet {
    private ShopTypeService shopTypeService = new ShopTypeServiceImp();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String op = req.getParameter("op");
        switch (op){
            case "get":
                String shopType = getShopType(req, resp);
                resp.setContentType("application/json;charset=utf-8");
                resp.getWriter().write(shopType);
                break;
            case "add":
                resp.getWriter().write(addShopType(req, resp));
                break;
            case "update":
                resp.getWriter().write(updateShopType(req, resp));
                break;
            case "del":
                resp.getWriter().write(delShopType(req, resp));
                break;
            default:
                resp.getWriter().write("");
                break;

        }
    }


    // 查询店铺类别数据
    public String getShopType(HttpServletRequest req, HttpServletResponse resp)  throws ServletException, IOException{
        List<ShopType> list = shopTypeService.getShopType();

        String json = new Gson().toJson(list);

        return json;
    }

    // 添加店铺类别数据

    public String addShopType(HttpServletRequest req, HttpServletResponse resp)  throws ServletException, IOException{
        String categoryName = req.getParameter("categoryName");

        return shopTypeService.addShopType(categoryName) + "";
    }

    // 修改店铺类别数据

    public String updateShopType(HttpServletRequest req, HttpServletResponse resp)  throws ServletException, IOException{
        String shopCategoryId = req.getParameter("shopCategoryId");
        String categoryName = req.getParameter("categoryName");

        return shopTypeService.updateShopType(shopCategoryId,categoryName) + "";
    }

    // 删除店铺类别数据

    public String delShopType(HttpServletRequest req, HttpServletResponse resp)  throws ServletException, IOException{
        String shopCategoryId = req.getParameter("shopCategoryId");

        return shopTypeService.delShopType(shopCategoryId) + "";
    }
}

